package com.wehealth.biz_ecg.adapter

import android.bluetooth.BluetoothDevice
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.wehealth.biz_ecg.R
import com.wehealth.biz_ecg.databinding.ItemLayoutBlueToothBinding
  import com.wehealth.biz_ecg.dialog.BlueToothDeviceListDialog

class BlueToothDeviceListAdapter(
    val dialog: BlueToothDeviceListDialog,
    val list: List<BluetoothDevice>,
    val connectAction: (device: BluetoothDevice) -> Unit
) :
    RecyclerView.Adapter<BlueToothDeviceListViewHolder>() {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BlueToothDeviceListViewHolder {
        val dataBinding = DataBindingUtil.inflate<ItemLayoutBlueToothBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_layout_blue_tooth,
            parent,
            false
        )
        return BlueToothDeviceListViewHolder(dataBinding.root, dataBinding)

    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: BlueToothDeviceListViewHolder, position: Int) {
        holder.bindData(dialog, list[position], connectAction)

    }
}

class BlueToothDeviceListViewHolder(view: View, val dataBinding: ItemLayoutBlueToothBinding) :
    RecyclerView.ViewHolder(view) {
    fun bindData(
        dialog: BlueToothDeviceListDialog,
        device: BluetoothDevice,
        connectAction: (device: BluetoothDevice) -> Unit
    ) {
        dataBinding.deviceInfo.text = "mac:${device.address} name:${device.alias}"
        dataBinding.root.setOnClickListener {
            dialog.dismiss()
            connectAction(device)
        }
    }
}